package com.njzxw.yoga.yogaWeb;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.UrlRewriteFilter;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import java.io.IOException;

@Configuration
public class ConfigNeWeb extends UrlRewriteFilter{

    private static final String URL_REWRITE = "classpath:/urlrewrite.xml";

    @Value(URL_REWRITE)
    private org.springframework.core.io.Resource resource;

    protected void loadUrlRewriter(FilterConfig filterConfig) throws ServletException {
        try {
            //Create a UrlRewrite Conf object with the injected resource
            Conf conf = new Conf(filterConfig.getServletContext(), resource.getInputStream(), resource.getFilename(), "@@traceability@@");
            checkConf(conf);
        } catch (IOException ex) {
            throw new ServletException("===Unable to load URL rewrite configuration file from " + URL_REWRITE, ex);
        }
    }

    @Bean
    public FilterRegistrationBean UrlRewritFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(new UrlRewriteFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("confReloadCheckInterval","0");
        registrationBean.addInitParameter("confReloadCheckEnabled","true");
        registrationBean.addInitParameter("logLevel","WARN");
        registrationBean.addInitParameter("confPath","urlrewrite.xml");
        return registrationBean;
    }

}
